package com.nf.teamwork.mall.service.impl;

import com.nf.teamwork.mall.dao.UserInfDao;
import com.nf.teamwork.mall.entity.UserInf;
import com.nf.teamwork.mall.entity.UserRoleRelevance;
import com.nf.teamwork.mall.service.UserInfService;
import com.nf.teamwork.mall.service.UserRoleRelevanceService;
import com.nf.teamwork.mall.utils.CodeUtil;
import com.nf.teamwork.mall.utils.MailUtil;
import com.nf.teamwork.mall.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: LJP
 * @Classname UserInfServiceImpl
 * @Date: 2020-02-27 19:14
 * @Description: 用户表service层
 */
@Service
public class UserInfServiceImpl implements UserInfService {
    @Autowired
    private UserInfDao userInfDao;
    @Autowired
    private UserRoleRelevanceService userRoleRelevanceService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;



    @Override
    public List<UserInf> findAll() {
        return userInfDao.findAll();
    }

    @Override
    public boolean register(UserInf userInf) {
        //生成一个11位长度的帐号
        String loginAccount = RandomCodeUtil.randomGenerate();

        //生成邮箱激活码
        String emailCode = CodeUtil.generateUniqueCode();

        //下面使用SpringSecurity提供的加密类对用户密码进行加密
        String encodePassword = bCryptPasswordEncoder.encode(userInf.getPassword());

        userInf.setPassword(encodePassword);
        userInf.setEmailCode(emailCode);
        userInf.setUsername(loginAccount);

        boolean result = userInfDao.register(userInf) > 0;

        if(result){
            try{
                //这里发送激活帐号的邮件给用户填入的邮箱
                new Thread(new MailUtil(userInf.getUserInfEmail(), emailCode, loginAccount)).start();
            }catch (Exception e){
                e.printStackTrace();
                //throw new RuntimeException("发送邮件出现异常");
                return false;
            }
        }
        return result;
    }

    @Override
    public UserInf findByAccount(String account) {
        return userInfDao.findByAccount(account);
    }

    @Override
    @Transactional
    public boolean activate(UserInf userInf) {
        /*激活操作*/
        boolean result = userInfDao.activate(userInf) > 0;
        if(result){
            UserRoleRelevance userRoleRelevance = new UserRoleRelevance();
            /*先根据目前用户账号来获取到用户对象信息*/
            UserInf byAccount = findByAccount(userInf.getUsername());
            userRoleRelevance.setUserInfId(byAccount.getUserInfId());
            /*这里的1是普通用户角色的id，也就是用户账号激活后默认是为普通用户角色*/
            userRoleRelevance.setRoleInfId(1);
            /*将用户与角色信息关联*/
            result = userRoleRelevanceService.add(userRoleRelevance);
        }
        return result;
    }


    /**
     * 根据账号查出用户对象信息
     * @param account 账号
     */
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        return userInfDao.findByAccount(account);
    }
}
